﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models;
using tracker.Models.BLL;
using tracker.Models.DAL;

namespace tracker.Controllers
{
    public class HandlerIsExistController : Controller
    {
        //
        // GET: /HandlerIsExist/
        //k-phu: Check user is exist
        [CallAjaxFilter]
        public JsonStringResult Index(string item, string type)
        {
            string result = "";
            bool isExist = false;
            if (String.IsNullOrEmpty(item) || String.IsNullOrEmpty(type))
            {
                isExist = true;
            }
            else
            {
                type = type.ToLower();

                
                if (type == "groupaccount" || type == "useraccount")
                {
                    // Check user
                    String tmp = item.ToLower();
                    if (tmp.Contains("admin") || tmp.Contains("tracker") || tmp.Contains("master"))
                    {
                        isExist = true;
                    }
                    else
                    {
                        // Check in database
                        EUserLoginBLL userBLL = new EUserLoginBLL();
                        isExist = userBLL.checkExist(item);
                    }
                }
                else if (type == "snimeiid")
                {
                    // Check device
                    DeviceDAL deviceDAL = new DeviceDAL();
                    if (deviceDAL.getByDeviceKey(item) != null)
                    {
                        isExist = true;
                    }
                }               
            }
            result = Utilities.createJSON("success", isExist.ToString().ToLower(), true);
            return new JsonStringResult(result);
        }

    }
}
